<?php
// 文章管理
namespace app\bbs_admin\controller;

use think\Controller;
use think\Db;
use app\bbs_admin\model\PermissionToRead\bbs_task_list;
use app\bbs_admin\model\PermissionToRead\bbs_jurisdiction_level;

class PermissionToRead extends Controller
{
  public function index()
  {
    $head = bbs_admin_menu_hend();
    $list = Db::table('bbs_jurisdiction_level')
      ->where('id != 1 and start = 0')
      ->select();
    $delete_list = Db::table('bbs_jurisdiction_level')
      ->where('id != 1 and start = 1')
      ->select();
    return $this->fetch('PermissionToReadIndex', ['head' => $head, 'list' => $list, 'delete_list' => $delete_list]);
  }
  public function permissiontoreadindexgetbbsjurisdictionleveldelete() //删除
  {
    $id = input('id');
    $calss = input('calss');
    if ($id) {
      $jurisdiction_number = Db::table('bbs_jurisdiction_level')
        ->where(['id' => $id])
        ->find();
      $start = Db::table('bbs_article')
        ->where(['permission_to_read' => $jurisdiction_number['jurisdiction_number']])
        ->count('id');
      if ($start) {
        $this->error("有文章阅读权限为 $jurisdiction_number[level_name] 禁止删除");
      } else {
        if ($calss == 'true') {
          $start = Db::table('bbs_jurisdiction_level')
            ->where(['id' => $id])
            ->delete();
          if ($start) {
            $this->success();
          } else {
            $this->error('原因未知');
          }
        } else {
          $start = Db::table('bbs_jurisdiction_level')
            ->where(['id' => $id])
            ->update(['start' => 1]);
          if ($start) {
            $this->success();
          } else {
            $this->error('原因未知');
          }
        }
      }
    } else {
      $this->error('ex me');
    }
  }
  public function permissiontoreadindexgetbbsjurisdictionlevel() //获取基本信息
  {
    $id = input('id');
    if ($id) {
      $info = Db::table('bbs_jurisdiction_level')
        ->where(['id' => $id])
        ->find();
      if ($info) {
        $this->success('success', null, $info);
      } else {
        $this->error('不存在id为' . $id . '的数据');
      }
    } else {
      $this->error('ex me');
    }
  }
  public function permissiontoreadindexgetbbsjurisdictionlevelrecover() //恢复
  {
    $id = input('id');
    if ($id) {
      $start = Db::table('bbs_jurisdiction_level')
        ->where(['id' => $id])
        ->update(['start' => 0]);
      if ($start) {
        $this->success();
      } else {
        $this->error('原因未知');
      }
    } else {
      $this->error('ex me');
    }
  }
  public function permissiontoreadindexgetbbsjurisdictionlevelsave() //保存等级信息
  {
    $id = input('id');
    if ($id) {
      $start = Db::table('bbs_jurisdiction_level')
        ->where(['id' => $id])
        ->update($_POST);
      if ($start) {
        $this->success();
      } else {
        $this->error('您未修改 或 发生意外错误');
      }
    } else {
      $this->error('ex me');
    }
  }
  public function new_jurisdiction_level_success() //新建
  {
    $_POST['add_time'] = date('Y-m-d H:i:s');
    $_POST['time'] = time();
    $_POST['start'] = '0';
    $_POST['id'] = Db::table('bbs_jurisdiction_level')
      ->insertGetId($_POST);
    if ($_POST['id']) {
      $this->success('success', null, $_POST);
    } else {
      $this->error('原因未知');
    }
  }
  public function publishing_tasks() //发布任务
  {
    $head = bbs_admin_menu_hend();
    $bbs_task_award_name = Db::table('bbs_task_award_name')
      ->where(['start' => 0])
      ->select();
    $bbs_jurisdiction_level = Db::table('bbs_jurisdiction_level')
      ->where(['start' => 0])
      ->select();
    return $this->fetch('publishing_tasks', ['head' => $head, 'bbs_jurisdiction_level' => $bbs_jurisdiction_level, 'bbs_task_award_name' => $bbs_task_award_name]);
  }
  public function article_select_select() //关键字提示
  {
    $class_select_input = input('keyword');
    if ($class_select_input) {
      $class_select_input_value['bbs_article.title'] = array('like', "%$class_select_input%");
    }
    $list = Db::table('bbs_article')
      ->where($class_select_input_value)
      ->alias('bbs_article')
      ->join('user user', 'bbs_article.user_id = user.id')
      ->field([
        'bbs_article.id',
        'bbs_article.title',
        'bbs_article.release_time',
        'user.user_name',
      ])
      ->select();
    if ($list) {
      $this->success('success', null, $list);
    } else {
      $this->error($list);
    }
  }
  public function publishing_tasks_form_success() //发布任务
  {
    $bbs_task_list = new bbs_task_list;
    $task_name = input('post.task_name');
    $extended_type = input('post.extended_type', 1);
    $extended_task_links = input('post.extended_task_links', 0);
    $article_id = input('post.article_id');
    $authority = input('post.authority');
    $bbs_user_integral = input('post.bbs_user_integral');
    $number = input('post.number');
    $mission_statement = input('post.mission_statement');
    //前台时间
    $datetimepicker_start = input('datetimepicker_start', date('Y年m月d日H:i', 0));
    $datetimepicker_end = input('datetimepicker_end',  date('Y年m月d日H:i', time()));
    $datetimepicker_start = date_parse_from_format('Y年m月d日H:i', $datetimepicker_start);
    date_default_timezone_set("PRC");
    $datetimepicker_start = mktime($datetimepicker_start["hour"], $datetimepicker_start["minute"], 0, $datetimepicker_start["month"], $datetimepicker_start["day"], $datetimepicker_start["year"]);
    $datetimepicker_end = date_parse_from_format('Y年m月d日H:i', $datetimepicker_end);
    date_default_timezone_set("PRC");
    $datetimepicker_end = mktime($datetimepicker_end["hour"], $datetimepicker_end["minute"], 0, $datetimepicker_end["month"], $datetimepicker_end["day"], $datetimepicker_end["year"]);
    //前台时间
    if ($datetimepicker_start == 943891200) {
      $datetimepicker_start = 0;
    }
    if ($datetimepicker_end == 943891200) {
      $datetimepicker_end = 0;
    }
    $once_a_day = input('post.once_a_day');
    switch ($extended_type) { //判断文章类型 0 扩展 1 本站
      case '0':
        if (!$extended_task_links) {
          $this::error('插入错误 错误行 ' . __LINE__);
        }
        break;

      case '1':
        if (!$article_id) {
          $this::error('插入错误 错误行 ' . __LINE__);
        }
        break;

      default:
        $this::error('类型错误 错误行 ' . __LINE__);
        break;
    }
    if (
      $task_name &&
      $authority &&
      $bbs_user_integral &&
      $number &&
      $mission_statement &&
      $datetimepicker_start &&
      $datetimepicker_end &&
      $once_a_day != null
    ) {
      $authority_int = ',';
      foreach (explode(',', $authority) as $key => $value) {
        $temp = Db::table('bbs_jurisdiction_level')
          ->where(['level_name' => trim($value)])->field('id')->find()['id'];
        $authority_int .= "$temp,";
      }
      try {
        //这个写法参考https://www.kancloud.cn/manual/thinkphp5/135188
        $bbs_task_list->task_name = $task_name;
        $bbs_task_list->article_id = $article_id;
        $bbs_task_list->extended_task_links = $extended_task_links;
        $bbs_task_list->authority = $authority_int;
        $bbs_task_list->mission_statement = $mission_statement;
        $bbs_task_list->datetimepicker_start = $datetimepicker_start;
        $bbs_task_list->datetimepicker_end = $datetimepicker_end;
        $bbs_task_list->once_a_day = $once_a_day;
        $bbs_task_list->add_time = date('Y-m-d H:i:s');
        $bbs_task_list->type = $extended_type;
        $bbs_task_list->bbs_user_integral = $bbs_user_integral;
        $bbs_task_list->number = $number;
        $bbs_task_list->start = 0;
        $bbs_task_list->save();
      } catch (\Exception $e) {
        $this::error('插入错误 错误行 ' . $e->getMessage());
      }
      $this::success('执行成功!');
    } else {
      $this::error('缺少插入必要参数');
    }
  }
  public function publishing_tasks_list()
  {
    $bbs_task_list = new bbs_task_list;
    $head = bbs_admin_menu_hend();
    // $list = bbs_task_list::chunk(100, function ($bbs_task_list) {
    //   foreach ($bbs_task_list as $user) {
    //     $user['aaaa'] = 'aaaaa';
    //     dump($user->toArray());
    //   }
    // });
    // $list = $bbs_task_list->alias("bbs_task_list")
    // ->join('bbs_article bbs_article','bbs_article.id = bbs_task_list.article_id')
    // ->select();
    /* 列表 */
    $list = $bbs_task_list::all(['start' => 0])->toArray();
    $bbs_jurisdiction_level_list = array();
    /* 获取所有用户组 */
    $bbs_jurisdiction_level = bbs_jurisdiction_level::all()->toArray();

    foreach ($bbs_jurisdiction_level as $key => $value) { // 不排除有删除的 所有吧所有的id 按照id重新排列
      $bbs_jurisdiction_level_list[$value['id']] = $value;
    }

    foreach ($list as $key => $value) {
      $temp = '';
      foreach (preg_grep("/\S+/i", explode(',', $value['authority'])) as $sonkey => $value) {
        $temp .= $bbs_jurisdiction_level_list[$value]['level_name'] . '|';
      }
      $list[$key]['authority'] = $temp;
    }
    /* 列表 */
    /* 已删除 */
    $list_delete = $bbs_task_list::all(['start' => 1])->toArray();

    foreach ($list_delete as $key => $value) {
      $temp = '';
      foreach (preg_grep("/\S+/i", explode(',', $value['authority'])) as $sonkey => $value) {
        $temp .= $bbs_jurisdiction_level_list[$value]['level_name'] . '|';
      }
      $list_delete[$key]['authority'] = $temp;
    }

    /* 已删除 */

    return $this->fetch(null, ['head' => $head, 'list' => $list, 'list_delete' => $list_delete]);
  }
  public function edit_publishing_tasks_html()
  {
    $id = $_GET['id'];
    if (!$id) {
      $this->error('error');
    } else {
      $bbs_task_list = Db::table('bbs_task_list')
        ->where(['bbs_task_list.id' => $id])
        ->alias("bbs_task_list")
        ->join('bbs_article bbs_article', 'bbs_task_list.article_id = bbs_article.id')
        ->field(['bbs_task_list.task_name', 'bbs_task_list.number', 'bbs_task_list.bbs_user_integral', 'bbs_task_list.id', 'bbs_task_list.extended_task_links', 'bbs_task_list.datetimepicker_start', 'bbs_task_list.datetimepicker_end', 'bbs_task_list.mission_statement', 'bbs_task_list.authority', 'bbs_article.title'])
        ->find();
    }

    $bbs_task_award_name = Db::table('bbs_task_award_name')
      ->where(['start' => 0])
      ->select();
    $bbs_user_integral = ''; //这是编辑框的下啦选择奖励框
    foreach ($bbs_task_award_name as $key => $value) { // 不排除有删除的 所有吧所有的id 按照id重新排列
      if ($bbs_task_list['bbs_user_integral'] == $value['id']) {
        $bbs_user_integral .= "<option value='$value[id]'> $value[award_name] </option>";
      } else {
        $bbs_user_integral .= "<option value='$value[id]'> $value[award_name] </option>";
      }
    }
    $number = $bbs_task_list['number']; //奖励的数值
    $bbs_jurisdiction_level = bbs_jurisdiction_level::all()->toArray(); //获取所有用户组
    foreach ($bbs_jurisdiction_level as $key => $value) { // 不排除有删除的 所有吧所有的id 按照id重新排列
      $bbs_jurisdiction_level_list[$value['id']] = $value;
    }
    $temp = array();
    foreach (preg_grep("/\S+/i", explode(',', $bbs_task_list['authority'])) as $key => $value) { //把所有选择的用户的 array 设置为真
      $temp[$value] = true;
    }
    $authority = '';
    foreach ($bbs_jurisdiction_level_list as $key => $value) { //循环全部的权限组
      if (isset($temp[$key]) == true) { // 遍历判断用户组的array 有没有 这个值 并且为真的
        $authority .= "<option value='$value[id]' selected='' >$value[level_name]</option>";
      } else {
        $authority .= "<option value='$value[id]' >$value[level_name]</option>";
      }
    }

    $bbs_jurisdiction_level['datetimepicker_start'] = date('Y年m月d日 H:i');
    $bbs_jurisdiction_level['datetimepicker_end'] = date('Y年m月d日 H:i');
    return <<<html
    <script src="/public/jquery/jquery-3.3.1.min.js" type="text/javascript"></script>
    <link rel="stylesheet" type="text/css" href="/public/bbs_admin/app-assets/css/bootstrap.css">
    <!-- font icons-->
    <link rel="stylesheet" type="text/css" href="/public/bbs_admin/app-assets/fonts/icomoon.css">
    <!-- END VENDOR CSS-->
    <!-- BEGIN ROBUST CSS-->
    <link rel="stylesheet" type="text/css" href="/public/bbs_admin/app-assets/css/bootstrap-extended.css">
    <!-- 富文本 -->
    <script charset="utf-8" src="/public/bbs/module/kindeditor/kindeditor-all.js"></script>
    <script charset="utf-8" src="/public/bbs/module/kindeditor/lang/zh-CN.js"></script>
    <!-- 富文本end -->
    <!-- data_time request -->
    <link href="http://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="/public/bbs_admin/data_time/assets/theme/css/tempusdominus-bootstrap-4.css" rel="stylesheet">
    <script type="text/javascript" src="/public/bbs_admin/data_time/js/moment-with-locales.min.js"></script>
    <script src="/public/bbs_admin/data_time/assets/theme/js/tempusdominus-bootstrap-4.js"></script>
    <script src="/public/bbs_admin/bootstrap/bootstrap/js/bootstrap.js"></script>
    <script src="/public/bbs_admin/bootstrap/bootstrap-select/js/bootstrap-select.js"></script>
    <link rel="stylesheet" href="/public/bbs_admin/bootstrap/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/public/bbs_admin/bootstrap/bootstrap-select/css/bootstrap-select.css">
    <link rel="stylesheet" type="text/css" href="/public/bbs_admin/datetime.css">

    <!-- data_time request end -->
    <!-- 多选框 -->
    <link rel="stylesheet" type="text/css" href="/public/bbs_admin/ArticleManagement/select/styles.css">
    <!-- 多选框end -->
    <script src="/public/layui/layui.all.js"></script>
    <link rel="stylesheet" href="/public/layui/css/layui.css">
        <form class="form">
          <div class="row">
              <div class="col-md-8 offset-md-2">
                  <div class="form-body">
                      <div class="form-group">
                          <label for="task_name">任务名</label>
                          <input type="text" id="task_name" value="$bbs_task_list[task_name]" class="form-control"
                              placeholder="任务名">
                      </div>
                      <div class="layui-tab layui-tab-card">
                          <ul class="layui-tab-title">
                              <li onclick="extended_type('1')" class="layui-this">本站任务</li>
                              <li onclick="extended_type('0')">指定链接</li>
                          </ul>
                          <div data-type="1" id='extended_type' class="layui-tab-content">
                              <div class="layui-tab-item layui-show">
                                  <div class="form-group">
                                      <label for="eventInput1">需要回复的文章</label>
                                      <div class="form-group position-relative">
                                          <input type="text"
                                              class="form-control form-control-lg input-lg com_search_input"
                                              id="title_and_id_of_article" data-id="$bbs_task_list[id]" value="$bbs_task_list[title]"
                                              placeholder="请填写关键字进行搜索 。。。  ">
                                          <div class="card">
                                              <div class="card-header">
                                                  <h4 class="card-title">提示</h4>
                                                  <a class="heading-elements-toggle"><i
                                                          class="icon-ellipsis font-medium-3"></i></a>
                                                  <div class="heading-elements">
                                                      <ul class="list-inline mb-0">
                                                          <li><a data-action="collapse"><i
                                                                      class="icon-minus4"></i></a>
                                                          </li>
                                                          <li><a data-action="reload"><i
                                                                      class="icon-reload"></i></a>
                                                          </li>
                                                          <li><a data-action="expand"><i
                                                                      class="icon-expand2"></i></a>
                                                          </li>
                                                          <li><a data-action="close"><i
                                                                      class="icon-cross2"></i></a>
                                                          </li>
                                                      </ul>
                                                  </div>
                                              </div>

                                              <div class="card-body collapse in">
                                                  <div class="card-block card-dashboard">
                                                      <div class="table-responsive">
                                                          <table style="text-align: center"
                                                              class="table">

                                                              <head>
                                                                  <tr>
                                                                      <td>id</td>
                                                                      <td>名称</td>
                                                                      <td>时间</td>
                                                                      <td>作者</td>
                                                                  </tr>
                                                              </head>
                                                              <tbody id='Tips'>
                                                                  <tr>
                                                                      <td>请填写关键字进行搜索</td>
                                                                      <td>请填写关键字进行搜索</td>
                                                                      <td>请填写关键字进行搜索</td>
                                                                      <td>请填写关键字进行搜索</td>
                                                                  </tr>
                                                              </tbody>
                                                          </table>
                                                      </div>
                                                  </div>
                                              </div>
                                          </div>
                                      </div>
                                  </div>
                              </div>
                              <div class="layui-tab-item">
                                  <label for="task_name">任务链接地址</label>
                                  <input type="text" id="extended_task_links" value='$bbs_task_list[extended_task_links]'
                                      class="form-control" placeholder="任务链接地址">
                              </div>
                          </div>
                      </div>




                      <div class="form-group">
                          <label for="eventInput2">任务权限范围</label>
                          <div class="row">
                              <div class="col-md-12">
                                  请选择权限
                                  <div style="float:left;width: 100%" class="form-group">
                                  <select name="class_list" class="selectpicker" multiple data-live-search="true"
                                      data-live-search-placeholder="搜索" data-actions-box="true">
                                      <optgroup label="权限列表">
                                      $authority
                                      </optgroup>
                                  </select>
                                  </div>
                              </div>
                          </div>
                      </div>

                      <div class="form-group">
                      <div class="col-md-6">
                          <label for="bbs_user_integral">请选择奖励</label>
                          <select id="bbs_user_integral" name="priority"
                              class="form-control" data-toggle="tooltip"
                              data-trigger="hover" data-placement="top"
                              data-title="Priority" data-original-title="" title="">
                              $bbs_user_integral
                          </select>
                      </div>
                      <div class="col-md-6">
                          <label for="number">输入奖励的金额/经验</label>
                          <input type="text" value='$number' id="number" class="form-control"
                              placeholder="输入奖励的金额/经验">
                      </div>
                  </div>

                      <div class="form-group">
                          <label for="eventInput3">任务说明</label>
                          <textarea id="mission_statement" name="content"
                              style="width:700px;height:300px;">
                              $bbs_task_list[mission_statement]
                          </textarea>
                      </div>

                      <div class="form-group">


                          <div class="col-md-6">
                              <label for="eventInput4">开始时间</label>
                              <div class="form-group">
                                  <!-- <label for="projectinput5">开始时间</label> -->
                                  <div class="container">
                                      <div class="row">
                                          <div>
                                              <div class="form-group">
                                                  <div class="input-group date"
                                                      data-target-input="nearest">
                                                      <input value='2018年1月24日 18:20' type="text"
                                                          id="datetimepicker_start"
                                                          name='datetimepicker_start'
                                                          class="form-control datetimepicker-input"
                                                          data-target="#datetimepicker_start" />
                                                      <div class="input-group-append"
                                                          data-target="#datetimepicker_start"
                                                          data-toggle="datetimepicker">
                                                          <div class="input-group-text">
                                                              <i class="fa fa-calendar"></i>
                                                          </div>
                                                      </div>
                                                  </div>
                                              </div>
                                          </div>
                                      </div>
                                  </div>
                              </div>
                          </div>
                          <div class="col-md-6">
                              <label for="eventInput5">结束时间</label>
                              <div class="form-group">
                                  <!-- <label for="projectinput5">开始时间</label> -->
                                  <div class="container">
                                      <div class="row">
                                          <div>
                                              <div class="form-group">
                                                  <div class="input-group date"
                                                      id="datetimepicker_end"
                                                      data-target-input="nearest">
                                                      <input type="text"
                                                          id="datetimepicker_end_input"
                                                          class="form-control datetimepicker-input"
                                                          data-target="#datetimepicker_end" />
                                                      <div class="input-group-append"
                                                          data-target="#datetimepicker_end"
                                                          data-toggle="datetimepicker">
                                                          <div class="input-group-text"><i
                                                                  class="fa fa-calendar"></i>
                                                          </div>
                                                      </div>
                                                  </div>
                                              </div>
                                          </div>
                                      </div>
                                  </div>
                              </div>
                          </div>
                      </div>

                      <div class="form-group">

                      </div>

                      <div class="col-md-12">
                          <fieldset style="text-align: center" data-once_a_day='once_a_day' id='once_a_day'
                              tabindex="0" class='radioGroup'>
                              <legend>每日一次:</legend>
                              <input id='0' name="group1" value="1" type="radio">
                              <label for='0'>是</label>
                              <input id='1' name="group1" value="0" type="radio">
                              <label for='1'>否</label>
                          </fieldset>
                      </div>

                  </div>
              </div>
          </div>

          <div class="form-actions center">
              <button type="button" onclick="form_success()" class="btn btn-primary">
                  <i class="icon-check2"></i> 提交
              </button>
          </div>
      </form>
      <script>
      $("[name$='group1']").click(function (e) {
        $('#once_a_day').data('once_a_day', e.currentTarget
            .id);
      });
      var editor ;
      KindEditor.ready(function (K) {
        editor = K.create('#mission_statement', {
              resizeType: 2,
              width: "100%",
              height: '393px',
              uploadJson: '{:url("upload/index/image_upload")}', // 相对于当前页面的路径
              newlineTag: 'br', //回车换行br|p
          });
      });
      $(function () {
          $('#datetimepicker_start')
              .datetimepicker({
                  locale: 'zh-cn'
              });
      });
      $(function () {
          $('#datetimepicker_end')
              .datetimepicker({
                  locale: 'zh-cn'
              });
      });
      $(document).ready(function(){ 
        $('#datetimepicker_start').val('$bbs_jurisdiction_level[datetimepicker_start]');
        $('#datetimepicker_end_input').val('$bbs_jurisdiction_level[datetimepicker_end]');
      }); 
      function extended_type(id) {
        $('#extended_type').data('type', id);
      }

      var cpLock = true;
      $('.com_search_input').on('compositionstart', function () {
          cpLock = false;
          //      console.log("compositionstart")
      });
      $('.com_search_input').on('compositionend', function () {
          cpLock = true;
          //      console.log("compositionend")
      });
      $(".com_search_input").on("input", function (e) {
          e.preventDefault();
          var _this = this;
          // console.log("input");
          setTimeout(function () {
              if (cpLock) {
                  $('#Tips').empty();
                  //开始写逻辑
                  $.ajax({
                      type: "POST",
                      url: "./article_select_select",
                      data: {
                          keyword: $('.com_search_input').val()
                      },
                      dataType: "JSON",
                      success: function (response) {
                          if (response.code) {
                              response.data.forEach(value => {
                                  $('#Tips').append(
                                      '<tr onclick="tisp_onclick(\'' + value
                                      .id + '\',\'' + value.title + '\')">' +
                                      '<td>' +
                                      value.id +
                                      '</td>' +
                                      '<td>' +
                                      value.title +
                                      '</td>' +
                                      '<td>' +
                                      value.release_time +
                                      '</td>' +
                                      '<td>' +
                                      value.user_name +
                                      '</td>' +
                                      '</tr>')
                              });
                          }
                      }
                  });
              }
          }, 0)
      })
      function tisp_onclick(id, name) {
        $('#title_and_id_of_article').val(name);
        $('#title_and_id_of_article').data('id', id);
      }
      function form_success(){

          var task_name = $('#task_name').val();
            var title_and_id_of_article = $('#title_and_id_of_article').data('id');
            var extended_task_links = $('#extended_task_links').val();
            var bbs_user_integral = $('#bbs_user_integral').val();
            var number = $('#number').val();
            var extended_type = $('#extended_type').data('type');
            var authority = $('#class_list_select').html();
            var mission_statement = editor.html();
            var datetimepicker_start = $('#datetimepicker_start').val();
            var datetimepicker_end = $('#datetimepicker_end_input').val();
            var once_a_day = $('#once_a_day').data('once_a_day');
            $.ajax({
              type: "POST",
              url: "./publishing_tasks_list_save",
              data: {
                  task_name: task_name,
                  extended_type: extended_type,
                  extended_task_links: extended_task_links,
                  article_id: title_and_id_of_article,
                  bbs_user_integral: bbs_user_integral,
                  number: number,
                  authority: authority,
                  mission_statement: mission_statement,
                  datetimepicker_start: datetimepicker_start,
                  datetimepicker_end: datetimepicker_end,
                  once_a_day: once_a_day,
                  id: $id,
              },
              dataType: "JSON",
              success: function (response) {
                  if (response.code) {
                      alert('保存成功');
                  } else {
                      alert(response.msg);                        
                  }
              }
          });
      }
      </script>
html;
  }
  public function publishing_tasks_list_save()
  {
    $bbs_task_list = new bbs_task_list;
    $id = input('id');
    $task_name = input('task_name');
    $extended_type = input('extended_type');
    $bbs_user_integral = input('bbs_user_integral');
    $number = input('number');
    $extended_task_links = input('extended_task_links');
    $article_id = input('article_id');
    $authority = input('authority');
    $mission_statement = input('mission_statement');
    $datetimepicker_start = input('datetimepicker_start');
    $datetimepicker_end = input('datetimepicker_end');
    $once_a_day = input('once_a_day');

    //前台时间
    $datetimepicker_start = input('datetimepicker_start', date('Y年m月d日H:i', 0));
    $datetimepicker_end = input('datetimepicker_end',  date('Y年m月d日H:i', time()));
    $datetimepicker_start = date_parse_from_format('Y年m月d日H:i', $datetimepicker_start);
    date_default_timezone_set("PRC");
    $datetimepicker_start = mktime($datetimepicker_start["hour"], $datetimepicker_start["minute"], 0, $datetimepicker_start["month"], $datetimepicker_start["day"], $datetimepicker_start["year"]);
    $datetimepicker_end = date_parse_from_format('Y年m月d日H:i', $datetimepicker_end);
    date_default_timezone_set("PRC");
    $datetimepicker_end = mktime($datetimepicker_end["hour"], $datetimepicker_end["minute"], 0, $datetimepicker_end["month"], $datetimepicker_end["day"], $datetimepicker_end["year"]);
    //前台时间
    if ($datetimepicker_start == 943891200) {
      $datetimepicker_start = 0;
    }
    if ($datetimepicker_end == 943891200) {
      $datetimepicker_end = 0;
    }
    switch ($extended_type) { //判断文章类型 0 扩展 1 本站
      case '0':
        if (!$extended_task_links) {
          $this::error('插入错误 错误行 ' . __LINE__);
        }
        break;

      case '1':
        if (!$article_id) {
          $this::error('插入错误 错误行 ' . __LINE__);
        }
        break;

      default:
        $this::error('类型错误 错误行 ' . __LINE__);
        break;
    }
    switch ($once_a_day) { //判断文章类型 0 扩展 1 本站
      case '0':
        break;
      case '1':
        break;
      default:
        $this::error('请完整的填写参数');
        break;
    }
    if (
      $task_name &&
      $authority &&
      $mission_statement &&
      $bbs_user_integral &&
      $number &&
      $datetimepicker_start &&
      $datetimepicker_end &&
      $once_a_day != null
    ) {
      $authority_int = ',';
      foreach (explode(',', $authority) as $key => $value) {
        $temp = Db::table('bbs_jurisdiction_level')
          ->where(['level_name' => trim($value)])->field('id')->find()['id'];
        $authority_int .= "$temp,";
      }
      try {
        //这个写法参考https://www.kancloud.cn/manual/thinkphp5/135188
        $bbs_task_list = bbs_task_list::get($id);
        $bbs_task_list->task_name = $task_name;
        $bbs_task_list->article_id = $article_id;
        $bbs_task_list->extended_task_links = $extended_task_links;
        $bbs_task_list->authority = $authority_int;
        $bbs_task_list->mission_statement = $mission_statement;
        $bbs_task_list->datetimepicker_start = $datetimepicker_start;
        $bbs_task_list->datetimepicker_end = $datetimepicker_end;
        $bbs_task_list->once_a_day = $once_a_day;
        $bbs_task_list->add_time = date('Y-m-d H:i:s');
        $bbs_task_list->type = $extended_type;
        $bbs_task_list->bbs_user_integral = $bbs_user_integral;
        $bbs_task_list->number = $number;
        $bbs_task_list->start = 0;
        $bbs_task_list->save();
      } catch (\Exception $e) {
        $this::error('插入错误 错误行 ' . $e->getMessage());
      }
      $this::success('执行成功!');
    } else {
      $this::error('缺少插入必要参数');
    }
  }
  public function publishing_tasks_list_recovery_publishing_tasks_list()
  {
    try {
      $id = input('id');
      $bbs_task_list = bbs_task_list::get($id);
      $bbs_task_list->start = 0;
      $bbs_task_list->save();
    } catch (\Exception $e) {
      $this::error('插入错误 错误行 ' . $e->getMessage());
    }
    $this::success('执行成功!');
  }
  public function publishing_tasks_list_delete_publishing_tasks_list()
  {
    try {
      $id = input('id');
      $bbs_task_list = bbs_task_list::get($id);
      $bbs_task_list->start = 1;
      $bbs_task_list->save();
    } catch (\Exception $e) {
      $this::error('插入错误 错误行 ' . $e->getMessage());
    }
    $this::success('执行成功!');
  }
  public function publishing_tasks_list_enter_delete_publishing_tasks_list()
  {
    try {
      $id = input('id');
      $bbs_task_list = bbs_task_list::get($id);
      $bbs_task_list->delete();
    } catch (\Exception $e) {
      $this::error('插入错误 错误行 ' . $e->getMessage());
    }
    $this::success('执行成功!');
  }
  public function publishing_tasks_list_open_authority()
  {
    $id = input('id');
    foreach (array_filter(explode(',', $id)) as $key => $value) {
      $bbs_jurisdiction_level[$key] = Db::table('bbs_jurisdiction_level')->where(['id' => $value])->field(['level_name,id'])->find();
    }
    $this->success($bbs_jurisdiction_level);
  }
}
